package com.ascent.ui;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import  com.ascent.util.TalkClient;

/**
 * 艾斯医药主框架界面
 * @author ascent
 * @version 1.0
 */
@SuppressWarnings("serial")
public class MainFrame extends JFrame {

	/**
	 * tabbed pane组件
	 */
	protected JTabbedPane tabbedPane;

	/**
	 * 产品 panel
	 */
	protected ProductPanel productPanel;
	
	protected JMenu fileMenu;

	/**
	 * 默认构造方法
	 */
	public MainFrame() {

		setTitle("欢迎使用AscentSys应用! ");

		Container container = this.getContentPane();
		container.setLayout(new BorderLayout());

		tabbedPane = new JTabbedPane();

		productPanel = new ProductPanel(this);
		tabbedPane.addTab("药品", productPanel);

		container.add(BorderLayout.CENTER, tabbedPane);

		JMenuBar myMenuBar = new JMenuBar();

		fileMenu = new JMenu("文件");

		JMenu openMenu = new JMenu("打开");
		JMenuItem localMenuItem = new JMenuItem("本地硬盘...");
		openMenu.add(localMenuItem);

		JMenuItem networkMenuItem = new JMenuItem("网络...");
		openMenu.add(networkMenuItem);

		JMenuItem webMenuItem = new JMenuItem("互联网...");
		openMenu.add(webMenuItem);
		fileMenu.add(openMenu);

		JMenuItem saveMenuItem = new JMenuItem("保存");
		fileMenu.add(saveMenuItem);

		JMenuItem exitMenuItem = new JMenuItem("退出");
		fileMenu.add(exitMenuItem);

		myMenuBar.add(fileMenu);

		exitMenuItem.addActionListener(new ExitActionListener());

		setupLookAndFeelMenu(myMenuBar);

		JMenu helpMenu = new JMenu("帮助");
		JMenuItem aboutMenuItem = new JMenuItem("关于");
		helpMenu.add(aboutMenuItem);

		myMenuBar.add(helpMenu);

		aboutMenuItem.addActionListener(new AboutActionListener());

        JMenuItem newItem = new JMenuItem("客服咨询");
		helpMenu.add(newItem);
		class NewMenuItemActionListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				// 在此处添加新菜单项的功能实现逻辑
				TalkClient client = new TalkClient();
				client.setVisible(true);
				try {
					client.connect("localhost", 1234);
				} catch (IOException ioException) {
					ioException.printStackTrace();
				}
				try {
					client.start();
				} catch (IOException ioException) {
					ioException.printStackTrace();
				}
			}
		}
		newItem.addActionListener(new NewMenuItemActionListener());

		this.setJMenuBar(myMenuBar);

		setSize(500, 400);
		setLocation(100, 100);
        
        JMenuItem reviewMenuItem = new JMenuItem("评价");
		reviewMenuItem.addActionListener(new review());
		helpMenu.add(reviewMenuItem);

		this.addWindowListener(new WindowCloser());

		fileMenu.setMnemonic('f');
		exitMenuItem.setMnemonic('x');
		helpMenu.setMnemonic('h');
		aboutMenuItem.setMnemonic('a');
		
	        JMenuItem addMenuItem = new JMenuItem("增加药品");
        addMenuItem.addActionListener(new AddActionListener());
        JMenuItem deleteMenuItem = new JMenuItem("删除药品");
        deleteMenuItem.addActionListener(new DeleteActionListener());
        JMenuItem changeMenuItem = new JMenuItem("修改药品");
        changeMenuItem.addActionListener(new ChangeActionListener());
        JMenuItem findMenuItem = new JMenuItem("查找药品");
        findMenuItem.addActionListener(new FindActionListener());

        fileMenu.add(addMenuItem);
        fileMenu.add(deleteMenuItem);
        fileMenu.add(changeMenuItem);
        fileMenu.add(findMenuItem);
        // 设定快捷键
        exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
                ActionEvent.CTRL_MASK));

        saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
                ActionEvent.CTRL_MASK));

        aboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
                ActionEvent.CTRL_MASK));
    }

    /*yeke 增加的界面*/  
    /*
    * 处理"增加"菜单监听器的内部类 
    */
    class AddActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            AddProduct addProduct = new AddProduct();
            addProduct.setVisible(true);
        }
    }

     
    /*
     * 处理"删除"菜单监听器的内部类
     */
    class DeleteActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            DeleteProduct deleteProduct = new DeleteProduct();
            deleteProduct.setVisible(true);
        }
    }
     
    /*
    * 处理"修改"菜单监听器的内部类
    */
    class ChangeActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            ChangeProduct changeProduct = new ChangeProduct();
            changeProduct.setVisible(true);
        }
    }
     
    /*
    * 处理"查找"菜单监听器的内部类
    */
    class FindActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            FindProduct findProduct = new FindProduct();
            findProduct.setVisible(true);
        }
    }
	/**
	 * 设定和选择外观
	 */
	protected void setupLookAndFeelMenu(JMenuBar theMenuBar) {

		UIManager.LookAndFeelInfo[] lookAndFeelInfo = UIManager
				.getInstalledLookAndFeels();
		JMenu lookAndFeelMenu = new JMenu("选项");
		JMenuItem anItem = null;
		LookAndFeelListener myListener = new LookAndFeelListener();

		try {
			for (int i = 0; i < lookAndFeelInfo.length; i++) {
				anItem = new JMenuItem(lookAndFeelInfo[i].getName() + " 外观");
				anItem.setActionCommand(lookAndFeelInfo[i].getClassName());
				anItem.addActionListener(myListener);

				lookAndFeelMenu.add(anItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		theMenuBar.add(lookAndFeelMenu);
	}
	
    /*
    * 处理"查找"菜单监听器的内部类
    */
    class FindActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            //FindProduct findProduct = new FindProduct();
        }
    }

	/**
	 * 退出方法.
	 */
	public void exit() {
		setVisible(false);
		dispose();
		System.exit(0);
	}

	/**
	 * "退出"事件处理内部类.
	 */
	class ExitActionListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			exit();
		}
	}

	/**
	 * 处理"关闭窗口"事件的内部类.
	 */
	class WindowCloser extends WindowAdapter {

		/**
		 * let's call our exit() method defined above
		 */
		public void windowClosing(WindowEvent e) {
			exit();
		}
	}

	/**
	 * 处理"外观"选择监听器的内部类
	 */
	class LookAndFeelListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String className = event.getActionCommand();
			try {
				UIManager.setLookAndFeel(className);
				SwingUtilities.updateComponentTreeUI(MainFrame.this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 处理"关于"菜单监听器的内部类
	 */
	class AboutActionListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String msg = "超值享受!";
			JOptionPane.showMessageDialog(MainFrame.this, msg);
		}
	}
}


/**
 * 处理"评价"菜单监听器的内部类
 */
class review implements ActionListener {
    public void actionPerformed(ActionEvent event) {
    	ProductReview productReview = new ProductReview();
    	productReview.setVisible(true);
    }
}